<#import "/spring.ftl" as spring />
<#assign ctx= request.contextPath/>
<!doctype html>
<!--[if lt IE 7]>
<html class="no-js lt-ie9 lt-ie8 lt-ie7"> <![endif]-->
<!--[if IE 7]>
<html class="no-js lt-ie9 lt-ie8"> <![endif]-->
<!--[if IE 8]>
<html class="no-js lt-ie9"> <![endif]-->
<!--[if gt IE 8]><!-->
<html class="no-js"> <!--<![endif]-->
<head>
    <#--    <base href="<@spring.url basePath/>">-->
    <#--    <meta charset="utf-8">-->
    <#--    <meta http-equiv="X-UA-Compatible" content="IE=edge">-->
    <#--    <title>Eureka</title>-->
    <#--    <meta name="description" content="">-->
    <#--    <meta name="viewport" content="width=device-width">-->

    <#--    <link rel="stylesheet" href="${ctx}/eureka/css/wro.css">-->
    <title>DownUp Service!</title>
    <meta charset="utf-8">
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width">
    <link rel="stylesheet" href="${ctx}/eureka/css/wro.css">
    <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
</head>

<body id="one">
<#include "header.ftl">
<div class="container-fluid xd-container">
    <h1>Instances currently registered with Eureka</h1>
    <table id='instances' class="table table-striped table-hover">
        <thead>
        <tr>
            <th>Application(keyidada)</th>
            <th>AMIs</th>
            <th>Availability Zones</th>
            <th>Status</th>
        </tr>
        </thead>
        <tbody>
        <#if apps?has_content>
            <#list apps as app>
                <tr>
                    <td><b>${app.name}</b></td>
                    <td>
                        <#list app.amiCounts as amiCount>
                            <b>${amiCount.key}</b> (${amiCount.value})<#if amiCount_has_next>,</#if>
                        </#list>
                    </td>
                    <td>
                        <#list app.zoneCounts as zoneCount>
                            <b>${zoneCount.key}</b> (${zoneCount.value})<#if zoneCount_has_next>,</#if>
                        </#list>
                    </td>
                    <td>
                        <#list app.instanceInfos as instanceInfo>
                        <#if instanceInfo.isNotUp>
                        <font color=red size=+1><b>
                                </#if>
                                <b>${instanceInfo.status}</b> (${instanceInfo.instances?size}) -
                                <#if instanceInfo.isNotUp>
                            </b></font>
                        </#if>
                        <#list instanceInfo.instances as instance>
                        <#if instance.isHref>
                            <a href="${instance.url}" target="_blank">${instance.id}</a>

                            <div id="sendUP${instance.id}">
                                <button id="sendUPBtn${instance.id}" onclick="sendUP('${instance.id}','${app.name}')">
<#--                                    <font color="#228b22">UP请求</font>-->
                                    UP:${instance.id?split(":")[1]}
<#--                                    <#if instance.id?length gt 5>${instance.id?substring(instance.id?length -4 ,instance.id?length)}<#else>${instance.id}</#if>-->
                                </button>
                                <div id="sendUPContent${instance.id}"></div>
                            </div>

                            <div id="sendDOWN${instance.id}">
                                <button id="sendDOWNBtn${instance.id}" onclick="sendDOWN('${instance.id}','${app.name}')">
<#--                                    <font color=red>DOWN请求</font>-->
                                    DOWN:${instance.id?split(":")[1]}
<#--                                    <#if instance.id?length gt 5>${instance.id?substring(instance.id?length -4 ,instance.id?length)}<#else>${instance.id}</#if>-->
                                </button>
                                <div id="sendDOWNContent${instance.id}"></div>
                            </div>
                            <#else>
                                ${instance.id}
                            </#if><#if instance_has_next>,</#if>
                            </#list>
                            </#list>
                    </td>
                </tr>
            </#list>
        <#else>
            <tr>
                <td colspan="4">No instances available</td>
            </tr>
        </#if>

        </tbody>
    </table>

    <#--      <h1>General Info</h1>-->

    <#--      <table id='generalInfo' class="table table-striped table-hover">-->
    <#--        <thead>-->
    <#--          <tr><th>Name</th><th>Value</th></tr>-->
    <#--        </thead>-->
    <#--        <tbody>-->
    <#--          <#list statusInfo.generalStats?keys as stat>-->
    <#--            <tr>-->
    <#--              <td>${stat}</td><td>${statusInfo.generalStats[stat]!""}</td>-->
    <#--            </tr>-->
    <#--          </#list>-->
    <#--          <#list statusInfo.applicationStats?keys as stat>-->
    <#--            <tr>-->
    <#--              <td>${stat}</td><td>${statusInfo.applicationStats[stat]!""}</td>-->
    <#--            </tr>-->
    <#--          </#list>-->
    <#--        </tbody>-->
    <#--      </table>-->

    <#--      <h1>Instance Info</h1>-->

    <#--      <table id='instanceInfo' class="table table-striped table-hover">-->
    <#--        <thead>-->
    <#--          <tr><th>Name</th><th>Value</th></tr>-->
    <#--        <thead>-->
    <#--        <tbody>-->
    <#--          <#list instanceInfo?keys as key>-->
    <#--            <tr>-->
    <#--              <td>${key}</td><td>${instanceInfo[key]!""}</td>-->
    <#--            </tr>-->
    <#--          </#list>-->
    <#--        </tbody>-->
    <#--      </table>-->
</div>
<script type="text/javascript" src="eureka/js/wro.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $('table.stripeable tr:odd').addClass('odd');
        $('table.stripeable tr:even').addClass('even');
    });

    function sendUP(instanceId,serviceName) {
        console.log("instanceId  " + instanceId);
        console.log("service   " + serviceName);
        $("#sendUPBtn" + instanceId).attr("disabled", true);
        // var serviceName = $("#cancelRestartIng"+index).parent().parent().siblings()[0].innerText;
        console.log("url  " + "${ctx}/eurekaService/sendDownUPForSpecificOne?serviceName= " + serviceName + "&instanceId=" + instanceId + " &status=UP",)
        $.ajax({
            url: "${ctx}/eurekaService/sendDownUPForSpecificOne?serviceName=" + serviceName + "&instanceId=" + instanceId + "&status=UP",
            type: 'get',
            contentType: false,  // 不用任何编码 因为formdata对象自带编码 django能够识别该对象
            processData: false,  // 告诉浏览器不要处理我的数据 直接发就行
            success: function (data) {
                // $("#sendUPBtn" + instanceId).attr("hidden", "hidden");
                text = document.getElementById('sendUPContent' + instanceId).innerText;
                text += '\n' + data;
                document.getElementById('sendUPContent' + instanceId).innerText = text;
            }
        })
    }

    function sendDOWN(instanceId,serviceName) {
        console.log("sendDOWN instanceId  " + instanceId);
        console.log("sendDOWN service   " + serviceName);
        console.log("url  " + "${ctx}/eurekaService/sendDownUPForSpecificOne?serviceName= " + serviceName + "&instanceId=" + instanceId + " &status=DOWN",)
        $.ajax({
            url: "${ctx}/eurekaService/sendDownUPForSpecificOne?serviceName=" + serviceName + "&instanceId=" + instanceId + "&status=DOWN",
            type: 'get',
            contentType: false,  // 不用任何编码 因为formdata对象自带编码 django能够识别该对象
            processData: false,  // 告诉浏览器不要处理我的数据 直接发就行
            success: function (data) {
                let ele = 'sendDOWNBtn'  + instanceId;
                console.log("ele   " + ele);
                $('#ele').attr("hidden", "hidden");
                console.log("btn  disabled " + $('#ele').attr("disabled"));
                console.log("btn  hidden  " + $("#sendDOWNBtn" + instanceId).attr("hidden"));
                text = document.getElementById('sendDOWNContent' + instanceId).innerText;
                text += '\n' + data;
                document.getElementById('sendDOWNContent' + instanceId).innerText = text;
            }
        })
    }
</script>
</body>
</html>
